package com.atguigu1.one;

/**
 * @author lhl
 * @date 2025/3/27 11:50
 */
public class OneArrayTest {

    public static void main(String[] args) {
        //1.数组的声明与初始化
        //静态初始化：数组变量的赋值与数组元素的赋值的操作同时进行
        double[] price;
        price = new double[]{21.2,22.3,23.4,25.2};

        //动态初始化：数组变量的赋值与数组元素的赋值的操作分开进行
        String[] foods = new String[4];

        //其他数组初始化方式
        int arr1[] = new int[4];
        int[] arr2 = {1,2,3,4};

        //2.数组元素的调用(通过角标的方式，获取数组的元素（角标的范围从0开始，到数组的长度-1结束）)
        System.out.println(price[0]);
        System.out.println(price[2]);
        //System.out.println(price[4]);//报异常：java.lang.ArrayIndexOutOfBoundsException

        foods[0] = "拌海蜇";
        foods[1] = "龙须菜";
        foods[2] = "炝冬笋";
        foods[3] = "玉兰片";

        //3.数组的长度：用来描述数组容器中容量的大小
        System.out.println(foods.length);
        System.out.println(price.length);

        //4.遍历数组
        for (int i = 0; i < foods.length; i++) {
            System.out.println(foods[i]);
        }

        for (int i = 0; i < price.length; i++) {
            System.out.println(price[i]);
        }
    }
}
